import {
  getServerSideCategories,
  getServerSideProducts,
} from '@/services/back';
import { getCategoryByPaths, parseUrlToFetchParams } from '@/utils/common';
import CategoriesPage from '@/views/Categories';
import { notFound } from 'next/navigation';

export async function generateMetadata({
  params,
}: {
  params: { slug: string[] };
}) {
  const { slug } = params;
  const categoryList = await getServerSideCategories();
  const targetCate = categoryList.find(
    (cate: any) => cate.format_paths === slug.join('/')
  );

  const cateTitle =
    targetCate?.title || slug[slug.length - 1] || 'DropshipZone';
  return {
    title: `${cateTitle} Dropship | Wholesale ${cateTitle} Online`,
    description:
      'Proudly owned and run by Australians, DropshipZone is a drop shipping provider who dedicates to offer you the easiest way to operate your online business. Our purchasing team aims at sourcing the finest product with the lowest price. Directly purchasing from the manufacturers globally, you can rest assured that you will save up to 80% off retail prices',
    keywords: ['DropshipZone', 'online', 'shopping.'],
  };
}

export default async function Page(props: any) {
  console.log('=========== start app/categories/page ===========');
  const categories = await getServerSideCategories();
  const slug = props?.params?.slug;
  // if (!slug) return notFound();
  const paths = slug?.map((p: any) => p.replace(/\.html?$/i, '')) || [];
  const currCategory = getCategoryByPaths(paths, categories);
  // console.log('💡💡:', paths.join('/'), '  categories:', categories?.length);
  if (paths?.length && !currCategory) {
    return notFound();
  }
  const pageTitle = currCategory?.title || 'Products';
  // const queryParams = parseUrlToFetchParams(
  //   'categories',
  //   paths,
  //   props?.searchParams,
  //   categories
  // );
  // console.log('💡 params >> ', queryParams);
  // const skusInfo = await getServerSideProducts(queryParams);
  // console.log('💡 result skus >>', skusInfo?.result?.length);
  const skusInfo = null;
  return (
    <CategoriesPage
      {...props}
      paths={paths}
      // filters={queryParams}
      pageTitle={pageTitle}
      category={currCategory}
      categories={categories || []}
      products={skusInfo || {}}
    />
  );
}
